package com.k2data.test.testng.parameterized;

import java.util.HashMap;
import java.util.Map;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ParamListener implements ITestListener {

	@Override
	public void onTestStart(ITestResult result) {
		System.out.println("on test start:  " +  getSomeInfo(result));
	}

	@Override
	public void onTestSuccess(ITestResult result) {
		System.out.println("on test success: " + getSomeInfo(result));
	}

	@Override
	public void onTestFailure(ITestResult result) {
		System.out.println("on test failure: " + getSomeInfo(result));
	}

	@Override
	public void onTestSkipped(ITestResult result) {
		System.out.println("test method skipped: " + getSomeInfo(result));
	}

	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
		System.out.println("test failed but within success %: " + getSomeInfo(result));
	}

	@Override
	public void onStart(ITestContext context) {
		System.out.println("on start of test: " + context.getName());
	}

	@Override
	public void onFinish(ITestContext context) {
		System.out.println("on finish of test: " + context.getName());
	}
	
	/**
	 * 
	 * @param result
	 * @return
	 */
	private static String getSomeInfo(ITestResult result){
		Map<String, Object> info = new HashMap<>();
		
		String methodName = result.getMethod().getConstructorOrMethod().getName();	
		info.put("methodName", methodName);
		
		
		Object[] parameters = result.getParameters();
		StringBuilder paramStr = new StringBuilder("[");
		for(Object param : parameters){
			paramStr.append(param + ";");
		}
		paramStr.append("]");
		
		info.put("methodParameters", paramStr.toString());

		Map<String, String> xmlParam = result.getTestContext().getCurrentXmlTest().getAllParameters();
		info.put("XMLParameters", xmlParam);
		
		
		return info.toString();
	}
}
